package com.fastloan.jn.mgt.customermanager.train.controller;

import com.fastloan.jn.mgt.customermanager.train.model.JnCustomerManangerTrain;
import com.fastloan.jn.mgt.customermanager.train.model.JnManagerTrain;
import com.fastloan.jn.mgt.customermanager.train.model.vo.ManagerTrainExt;
import com.fastloan.jn.mgt.customermanager.train.service.JnCustomerManagerTrainService;
import com.fastloan.jn.mgt.customermanager.train.service.JnManagerTrainService;
import com.fastloan.jn.utils.other.ObjUtil;
import com.fastloan.safe.core.util.ManagerKit;
import com.fastloan.safe.mgt.org.model.CmuOrg;
import com.fastloan.safe.mgt.org.service.CmuOrgService;
import com.fastloan.safe.mgt.user.model.CmuUser;
import com.fastloan.safe.mgt.user.service.CmuUserService;
import com.kawins.base.baseabs.BaseController;
import com.kawins.result.ResultTo;
import org.kie.api.task.model.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;
import tk.mybatis.mapper.entity.Example;

import java.util.*;
import java.util.stream.Collectors;

/**
 * Created by gelon on 2017/10/26.
 */
@RestController
@RequestMapping("/api/web/customerManagerTrain")
public class JnManagerTrainController extends BaseController<JnCustomerManangerTrain>{

    @Autowired
    JnManagerTrainService managerTrainService ;

    @Autowired
    JnCustomerManagerTrainService customerManagerTrainService;

    @Autowired
    CmuOrgService orgService ;

    @Autowired
    CmuUserService userService;



    @PostMapping("/allTrain")
    public ResultTo allTrain(){
        Example example = new Example(JnCustomerManangerTrain.class);
        example.createCriteria().andEqualTo("orgId", ManagerKit.getOrgId());
        return dataPage(example);
    }

    @RequestMapping("/saveUpdate")
    @Transactional
    public ResultTo saveUpdae(JnCustomerManangerTrain jnCustomerManangerTrain){
        if(ObjUtil.isEmpty(jnCustomerManangerTrain.getId())){
            jnCustomerManangerTrain.setCreateTime(new Date());
            jnCustomerManangerTrain.setCreater(ManagerKit.getUserId());
            jnCustomerManangerTrain.setOrgId(ManagerKit.getOrgId());
            customerManagerTrainService.insertSelective(jnCustomerManangerTrain);
        } else {
            customerManagerTrainService.updateByPrimaryKeySelective(jnCustomerManangerTrain);
        }
        return new ResultTo().setData(jnCustomerManangerTrain);
    }


    @GetMapping("/train/{trainId}")
    public ResultTo getTrain(@PathVariable("trainId") String trainId ){
        return new ResultTo().setData(customerManagerTrainService.selectByPrimaryKey(trainId));
    }



    @PostMapping("/updateManagerTrain")
    public ResultTo updateManagerTrain(JnManagerTrain jnManagerTrain){
        JnManagerTrain dbManagerTrain = managerTrainService.selectOne(new JnManagerTrain()
                .setTrainId(jnManagerTrain.getTrainId())
                .setUserId(jnManagerTrain.getUserId()));
        jnManagerTrain.setId(dbManagerTrain.getId());
        managerTrainService.updateByPrimaryKeySelective(jnManagerTrain);
        return new ResultTo();
    }

    @GetMapping("/getManagerTrain")
    public ResultTo getManagerTrain(String userId,String trainId){
        JnManagerTrain jnManagerTrain = managerTrainService.selectOne(new JnManagerTrain().setUserId(userId).setTrainId(trainId));
        return new ResultTo().setData(jnManagerTrain);
    }





    @PostMapping("/changeTrainManagers")
    public ResultTo changeManagers(String[] addIds,String[] removeIds,String trainId ){
        managerTrainService.changeTrainManagers(addIds, removeIds, trainId, ManagerKit.getUserId());
        return new ResultTo();
    }


    @GetMapping("/allOrg")
    public ResultTo allOrg(){
        List<CmuOrg> orgs = orgService.selectAll();
        return new ResultTo().setData(orgs);
    }

    @PostMapping("/trainManagers")
    public ResultTo getTrainManager(String trainId , String cname){
        Map<String,Object> map = new HashMap<>();
        map.put("trainId",trainId);
        if(!ObjUtil.isEmpty(cname)) {map.put("cname",cname.replace("#",""));}
        return dataPage(managerTrainService,ManagerTrainExt.class,"selectManagerTrain",map);
    }



    @GetMapping("/trainManagersExt/{trainId}")
    public ResultTo trainManagers(@PathVariable("trainId") String trainId){
        List<JnManagerTrain> jnManagerTrain = managerTrainService.select(new JnManagerTrain().setTrainId(trainId));
        List<CmuUser> users = jnManagerTrain.stream().map(this::userFromManagerTrain).collect(Collectors.toList());
        users.removeAll(Collections.singleton(null));
        return new ResultTo().setData(users);
    }

    public CmuUser userFromManagerTrain(JnManagerTrain managerTrain){
        return userService.selectByPrimaryKey(managerTrain.getUserId());
    }

}
